# -*- coding : utf-8 -*-
# @Time    : 2023/12/07 17:20
# @Author  : wanggang
# @Email   : wanggang2@lingxi.ai
# @File    : customer_info_server.py
# @Software: LLM_internal

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #



import json
import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
import tornado.httpclient
from tornado import netutil, httpserver, ioloop, web, process
from tornado.options import define
from tornado.options import options

# from customerInfo import settings
# from customerInfo.utils import data_handling
import settings
from utils import data_handling

data_handler = data_handling.DataHandle()
define("port", default=settings.PORT, help="run on the given port", type=int)

class customerInfoHandle(tornado.web.RequestHandler):


    def run(self, customer_id):

        customer_info = data_handler.get_customer_info(customer_id)
        return customer_info

    def get(self):

        customer_id = self.get_argument('customer_id', '')
        customer_info = self.run(customer_id)

        self.write(json.dumps(customer_info))



class Handle(tornado.web.RequestHandler):

    # 功能函数
    def run(self):
        pass

    # get请求入口
    def get(self):
        self._get_queries()

    # post请求入口
    def post(self):
        self._post_queries()

    # get请求获取参数
    def _get_queries(self):
        arg_1 = self.get_argument('arg_1', '')
        arg_2 = self.get_argument('arg_2', '')

        return arg_1, arg_2

    # post请求获取灿谁
    def _post_queries(self):
        arg = json.loads(self.request.body)
        arg_1 = arg['arg_1']
        arg_2 = arg['arg_2']

        return arg_1, arg_2



if __name__ == '__main__':
    # 启动服务
    # 2 A100 支持 QPS 10 process
    # 单卡多进程
    # 单次多请求 batch 发送逻辑
    settings.log.info("启动服务...")
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[
        (r'/original', Handle),
        (r'/customerInfo', customerInfoHandle),
        ],
        # autoreload=False,
        debug=False,
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.bind(options.port)
    http_server.start(settings.PROCESSING_NUM)

    # sockets = tornado.netutil.bind_sockets(settings.PORT)
    # tornado.process.fork_processes(settings.PROCESSING_NUM)
    # http_server.add_sockets(sockets)

    # app.listen(8899)
    settings.log.info("端口：{}...".format(settings.PORT))
    tornado.ioloop.IOLoop.instance().start()















